import json
import time

import requests
from Cipher_AES import decrypt,encrypt
import Utils
import os
from update_module import url_map

version = "v1.3"





f = requests.get("https://remote-data.oss-cn-shanghai.aliyuncs.com/data/cc3buildTool-version.json",timeout=2)
version_info = json.loads(f.text)

root_dir = os.getcwd()
upgrade_dir = f'{root_dir}/upgrade/{version}'

if os.path.exists(f'{root_dir}/bin'):
   Utils.removeFile(f'{root_dir}/bin')
os.makedirs(f'{root_dir}/bin')

print("\n\n========This update contains the following==========\n")
files = os.listdir(upgrade_dir)
ver = version[1:]
for file in files:
    src = f'{upgrade_dir}/{file}'
    dist = f'{root_dir}/bin/{url_map[file]}.wasm'
    if os.path.exists(dist):
        os.remove(dist)
    Utils.writeInFile(dist,encrypt(Utils.fromFile(src)))
    version_info[url_map[file]]['ver'] = ver
    print(f'>upgrade file: {file}\n>out path: {dist}')
    print(f'ver: {ver}\n')
print("\n========This update contains the following==========\n\n")

Utils.writeDictInFile(f'{root_dir}/bin/cc3buildTool-version.json',version_info)